package gui.standard.form;

import java.sql.SQLException;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.tablemodel.PoslovniPartnerTableModel;
import net.miginfocom.swing.MigLayout;
import util.components.JLimitedTextField;

public class PoslovniPartnerForm extends StandardForm {

	private static final long serialVersionUID = 1L;
	private JLimitedTextField tfSifraPosPartnera;
	private JLimitedTextField tfNazivPosPartnera;

	public PoslovniPartnerForm() {

	}
	
	@Override
	public void init(){
		
		super.init();
		
		tfSifraPosPartnera = new JLimitedTextField(12, 12, true);
		tfNazivPosPartnera = new JLimitedTextField(30, 30, true);
		
		setTitle("Poslovni partner");
		
		tableModel = new PoslovniPartnerTableModel(
				new String[] { "Sifra pos. partnera", "Naziv pos. partnera" }, 0);
		
		tblGrid.setModel(tableModel);
		
		children.add(new PrometniDokumentForm());
		
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new MigLayout("fillx"));
		JPanel dataPanel = new JPanel();
		dataPanel.setLayout(new MigLayout("gapx 15px"));

		JLabel lblSifraPosPartnera = new JLabel("Sifra pos. partnera:");
		JLabel lblNazivPosPartnera = new JLabel("Naziv pos. partnera:");

		dataPanel.add(lblSifraPosPartnera);
		dataPanel.add(tfSifraPosPartnera, "wrap");
		dataPanel.add(lblNazivPosPartnera);
		dataPanel.add(tfNazivPosPartnera, "pushx");
		bottomPanel.add(dataPanel);

		bottomPanel.add(buttonsPanel, "dock east");

		add(bottomPanel, "grow, wrap");
	}

	@Override
	public void sync() {
		// TODO Auto-generated method stub
		int index = tblGrid.getSelectedRow();
		if (index < 0) {
			tfSifraPosPartnera.setText("");
			tfNazivPosPartnera.setText("");
			return;
		}
		String sifra = (String) tableModel.getValueAt(index, 0);
		String naziv = (String) tableModel.getValueAt(index, 1);

		tfSifraPosPartnera.setText(sifra);
		tfNazivPosPartnera.setText(naziv);

		setMode(StandardForm.MODE_EDIT);

	}

	@Override
	public void addRow() {
		// TODO Auto-generated method stub
		String sifra = tfSifraPosPartnera.getText().trim();
		String naziv = tfNazivPosPartnera.getText().trim();
		try {
			int index = tableModel.insertRow(new String[] { sifra, naziv });
			tblGrid.setRowSelectionInterval(index, index);
			setMode(MODE_ADD);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void searchRow() {
		// TODO Auto-generated method stub
		String sifra = tfSifraPosPartnera.getText().trim();
		String naziv = tfNazivPosPartnera.getText().trim();
		try {
			tableModel.searchRows(new String[] { sifra, naziv });
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void editRow() {
		// TODO Auto-generated method stub
		String sifra = tfSifraPosPartnera.getText().trim();
		String naziv = tfNazivPosPartnera.getText().trim();
		try {
			int index = tableModel.editRow(new String[] { sifra, naziv },
					tblGrid.getSelectedRow());
			tblGrid.setRowSelectionInterval(index, index);
		} catch (SQLException ex) {
			JOptionPane.showMessageDialog(this, ex.getMessage(), "Greska",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	@Override
	public void setMode(int mode) {
		super.setMode(mode);

		if (mode != StandardForm.MODE_ADD)
			return;

		tfSifraPosPartnera.setText("");
		tfNazivPosPartnera.setText("");
	}

	@Override
	public String[] getSelectedRowKeys() {
		// TODO Auto-generated method stub
		return new String[]{tfSifraPosPartnera.getText().trim()};
	}

	@Override
	public void getZoomedCol(String[] keys, StandardForm form) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String[] getSelectedRowNextKeys(int index) {
		// TODO Auto-generated method stub
		return new String[]{"", "", "", "", "", "", tfSifraPosPartnera.getText().trim(), "", "", "", "", "", ""};
	}

}
